# Maintainer: Felix Yan <felixonmars@archlinux.org>
# Contributor: Arch Haskell Team <arch-haskell@haskell.org>

pkgname=stylish-haskell
pkgver=0.13.0.0
pkgrel=81
pkgdesc="Haskell code prettifier"
url="https://github.com/haskell/stylish-haskell"
license=("BSD")
arch=('loong64' 'x86_64')
depends=('ghc-libs' 'haskell-aeson' 'haskell-file-embed' 'haskell-ghc-lib-parser'
         'haskell-optparse-applicative' 'haskell-strict' 'haskell-syb' 'haskell-hsyaml'
         'haskell-hsyaml-aeson')
makedepends=('ghc' 'uusi' 'haskell-hunit' 'haskell-random' 'haskell-test-framework'
             'haskell-test-framework-hunit')
source=("https://hackage.haskell.org/packages/archive/${pkgname}/${pkgver}/${pkgname}-${pkgver}.tar.gz")
sha512sums=('6a575568f9e81bf840743282d65a48f9bfa6541f192aab7633c93f8a6805e95897c4137cb70aaf5fc341db850b435da0b9b60e60e505b3fa8f90c2ce5ed8d075')

prepare() {
    cd $pkgname-$pkgver
    uusi -u Cabal -u aeson -u optparse-applicative $pkgname.cabal
}

build() {
    cd $pkgname-$pkgver
    
    runhaskell Setup configure -O --enable-shared --enable-executable-dynamic --disable-library-vanilla \
        --prefix=/usr --docdir=/usr/share/doc/$pkgname --datasubdir=$pkgname --enable-tests --datasubdir=$pkgname \
        --dynlibdir=/usr/lib --libsubdir=\$compiler/site-local/\$pkgid --ghc-option=-fllvm \
            -f-old_base --ghc-option='-pie'
    runhaskell Setup build $MAKEFLAGS
    runhaskell Setup register --gen-script
    runhaskell Setup unregister --gen-script
    sed -i -r -e "s|ghc-pkg.*update[^ ]* |&'--force' |" register.sh
    sed -i -r -e "s|ghc-pkg.*unregister[^ ]* |&'--force' |" unregister.sh
}

check() {
    cd $pkgname-$pkgver
    # https://github.com/haskell/stylish-haskell/issues/370
    runhaskell Setup test --show-details=direct || echo "Tests failed"
}

package() {
    cd $pkgname-$pkgver
    
    install -D -m744 register.sh   "${pkgdir}/usr/share/haskell/register/${pkgname}.sh"
    install -D -m744 unregister.sh "${pkgdir}/usr/share/haskell/unregister/${pkgname}.sh"
    runhaskell Setup copy --destdir="${pkgdir}"
    install -D -m644 LICENSE "$pkgdir"/usr/share/licenses/$pkgname/LICENSE
    rm -f "$pkgdir"/usr/share/doc/$pkgname/LICENSE
}
